package com.yaodian100.core.common.utils;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Eric Xu
 * 
 */
public final class ImageUtils {
	protected final static Logger logger = LoggerFactory.getLogger(ImageUtils.class);// NOPMD
	// 方形主檔圖500*500
	public static int main500ImgLen = 500;
	public static int main500ImgWidth = 500;

	// 方形主檔圖400*400
	public static int main400ImgLen = 400;
	public static int main400ImgWidth = 400;

	// 方形主檔圖250*250
	public static int main250ImgLen = 250;
	public static int main250ImgWidth = 250;

	// 方形主檔圖135*135
	public static int main135ImgLen = 135;
	public static int main135ImgWidth = 135;

	// 方形主檔圖100*100
	public static int main100ImgLen = 100;
	public static int main100ImgWidth = 100;

	// 方形主檔圖75*75
	public static int main75ImgLen = 75;
	public static int main75ImgWidth = 75;

	// 方形主檔圖50*50
	public static int main50ImgLen = 50;
	public static int main50ImgWidth = 50;

	// 长形主檔圖400x530
	public static int rmain530ImgLen = 530;
	public static int rmain400ImgWidth = 400;

	// 长形主檔圖250x330
	public static int rmain330ImgLen = 330;
	public static int rmain250ImgWidth = 250;

	// 长形主檔圖170x225
	public static int rmain225ImgLen = 225;
	public static int rmain170ImgWidth = 170;

	// 长形主檔圖135x180
	public static int rmain180ImgLen = 180;
	public static int rmain135ImgWidth = 135;

	// 长形主檔圖100x130
	public static int rmain130ImgLen = 130;
	public static int rmain100ImgWidth = 100;

	// 长形主檔圖70x93
	public static int rmain93ImgLen = 93;
	public static int rmain70ImgWidth = 70;

	// 說明大圖
	// static int descBigImgLen = 620;// 不限長度
	public static int descBigImgWidth = 700;

	// 說明小圖
	// static int descSmallImgLen = 100;
	// static int descSmallImgWidth = 100;

	// 商品大圖
	public static int productBigLen = 620;
	public static int productBigWidth = 520;

	// 商品小圖
	public static int productSmallLen = 50;
	public static int productSmallWidth = 50;

	// static String filePath =
	// "d:\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp1\\wtpwebapps\\yaodian100-midend\\ProductImg\\";
	// static String filePath = "c:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\PImg\\";
	static String subName = ".jpg";
	public static float compressRatio = 0.9f;

	// icon40*40
	// public static int iconImgLen = 40;
	// public static int iconImgWidth = 40;
	public static int iconImgLen = 60;
	public static int iconImgWidth = 60;

	// 方形主檔圖400*400
	public static int mainSpecImgLen = 75;
	public static int mainSpecImgWidth = 75;

	// 方形主檔圖400*400
	public static int rmainSpecImgLen = 99;
	public static int rmainSpecImgWidth = 75;

	/**
	 * 說明圖
	 * 
	 * @param filePath
	 * @param bb
	 */
	public static void descResize(final String prodId, final BufferedImage bufferedImage, final String filename,
			String subName, String filePath) {
		// resize(prodId, bufferedImage, descBigImgLen, descBigImgWidth, true, filename, subName, filePath);
		// resizePress(prodId, bufferedImage, bufferedImage.getHeight(), descBigImgWidth, false, filename, subName,
		// filePath,
		// null);
		resizePressAveraging(prodId, bufferedImage, bufferedImage.getHeight(), descBigImgWidth, false, filename, subName,
				filePath, null);
	}

	/**
	 * 商品大圖/小圖
	 * 
	 * @param filePath
	 * @param bb
	 */
	public static void prdtResize(final String prodId, final BufferedImage bufferedImage, final String filename,
			String subName, String filePath) {
		// resize(prodId, bufferedImage, main50ImgLen, main50ImgWidth, true, filename + "_s", subName, filePath);
		// resize(prodId, bufferedImage, main500ImgLen, main500ImgWidth, true, filename + "_b", subName, filePath);
		// resizePress(prodId, bufferedImage, productBigLen, productBigWidth, true, filename + "_b", subName, filePath,
		// "Y");
		// resizePress(prodId, bufferedImage, productSmallLen, productSmallWidth, true, filename + "_s", subName, filePath,
		// "Y");
		resizePressAveraging(prodId, bufferedImage, productBigLen, productBigWidth, false, filename + "_b", subName,
				filePath, "Y");
		resizePressAveraging(prodId, bufferedImage, productSmallLen, productSmallWidth, false, filename + "_s", subName,
				filePath, "Y");
	}

	public static int getHeight(String fileName) {
		int height = 0;
		Image image;
		try {
			image = ImageIO.read(new File(fileName));
			height = image.getHeight(null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return height;
	}

	public static int getLength(String text) {
		int length = 0;
		for (int i = 0; i < text.length(); i++) {
			if (new String(text.charAt(i) + "").getBytes().length > 1) {
				length += 2;
			} else {
				length += 1;
			}
		}
		return length / 2;
	}

	public static int getWidth(String fileName) {
		int width = 0;
		Image image;
		try {
			image = ImageIO.read(new File(fileName));
			width = image.getWidth(null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return width;
	}

	public static void customChange(int width, int height, boolean bb, final BufferedImage img, String fileWholePath,
			boolean pressImage) {

		boolean isSuccess = false;
		BufferedImage bi = img;
		File tempFile = null;

		try {
			if (pressImage) {// 浮水印
				tempFile = pressImage(img, 0, 0, 1f, fileWholePath + ".temp");
				bi = ImageIO.read(tempFile);
				if (tempFile.exists()) {
					tempFile.delete();
				}
			}
			double ratio = 0.0; // 縮放比例

			Image itemp = img;
			// 計算比例

			double diffH = Integer.valueOf(height).doubleValue() / Integer.valueOf(bi.getHeight()).doubleValue();
			double diffW = Integer.valueOf(width).doubleValue() / Integer.valueOf(bi.getWidth()).doubleValue();

			if (diffH < 1 || diffW < 1) {// 只針對比設定尺寸大的做縮圖動作
				if (diffH <= diffW) {

					ratio = Integer.valueOf(height).doubleValue() / bi.getHeight();

					itemp = bi.getScaledInstance(new Double(img.getWidth() * ratio).intValue(), new Double(img.getHeight()
							* ratio).intValue(), bi.SCALE_AREA_AVERAGING);
				} else if (diffH > diffW) {
					ratio = Integer.valueOf(width).doubleValue() / bi.getWidth();

					itemp = bi.getScaledInstance(new Double(img.getWidth() * ratio).intValue(), new Double(img.getHeight()
							* ratio).intValue(), bi.SCALE_AREA_AVERAGING);
				}

			}
			if (bb) {
				BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
				Graphics2D g = image.createGraphics();
				g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
				g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
				g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g.setColor(Color.white);
				g.fillRect(0, 0, width, height);
				if (width == itemp.getWidth(null)) {
					g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null),
							Color.white, null);
				} else if (height == itemp.getHeight(null)) {
					g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null),
							Color.white, null);
				} else {
					g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, (height - itemp.getHeight(null)) / 2, itemp
							.getWidth(null), itemp.getHeight(null), Color.white, null);
				}
				g.dispose();
				itemp = image;
			} else {

				BufferedImage image = new BufferedImage(itemp.getWidth(null), itemp.getHeight(null), BufferedImage.TYPE_INT_BGR);
				Graphics2D g = image.createGraphics();

				g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
				g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
				g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
				g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

				g.drawImage(itemp, 0, 0, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
				g.dispose();
				itemp = image;
			}

			Iterator<ImageWriter> it = ImageIO.getImageWritersBySuffix("jpg");
			if (it.hasNext()) {
				FileImageOutputStream fileImageOutputStream = new FileImageOutputStream(new File(fileWholePath));
				ImageWriter iw = it.next();
				ImageWriteParam iwp = iw.getDefaultWriteParam();
				iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
				iwp.setCompressionQuality(compressRatio);
				iw.setOutput(fileImageOutputStream);
				iw.write(null, new IIOImage((BufferedImage) itemp, null, null), iwp);
				iw.dispose();
				fileImageOutputStream.flush();
				fileImageOutputStream.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws IOException {
		// pressImage(ServerValue.getMidApachePath() + File.separator + "new_image.png", "D:\\win7.jpg", 0, 00, 0.5f);
		// pressImage("d:\\WWW.htdocs\\ok.png", "d:\\WWW.htdocs\\test.png", 0, 00, 0.5f);

		String imgList[] = { "yd04a-C550410-1-500.jpg", "yd04a-C550410-2-500.jpg", "yd04a-C550410-3-500.jpg" };

		for (int i = 0; i < imgList.length; i++) {

			// Image image = javax.imageio.ImageIO.read(new File("d:\\WWW.htdocs\\sample\\" + imgList[i]));
			// BufferedImage bufferedImage = new BufferedImage(image.getHeight(null), image.getWidth(null),
			// BufferedImage.TYPE_INT_RGB);
			// bufferedImage.getGraphics().drawImage(image, 0, 0, image.getHeight(null), image.getWidth(null), null); //
			// 绘制缩小后的图

			BufferedImage bufferedImage = ImageIO.read(new File("d:\\WWW.htdocs\\sample\\test\\" + imgList[i]));
			String prodId = "000000090";
			String filePath = "d:\\WWW.htdocs\\";
			String subName = "jpg";
			resizePressAveraging(prodId, bufferedImage, main500ImgLen, main500ImgWidth, true, "m500_aver_" + i, subName,
					filePath, "Y");
			resizePressAveraging(prodId, bufferedImage, main250ImgLen, main250ImgWidth, true, "m250_aver_" + i, subName,
					filePath, "Y");
			resizePressAveraging(prodId, bufferedImage, main135ImgLen, main135ImgWidth, true, "m135_aver_" + i, subName,
					filePath, "Y");
			resizePressAveraging(prodId, bufferedImage, main100ImgLen, main100ImgWidth, true, "m100_aver_" + i, subName,
					filePath, "Y");
			resizePressAveraging(prodId, bufferedImage, main75ImgLen, main75ImgWidth, true, "m75_aver_" + i, subName,
					filePath, "Y");
			resizePressAveraging(prodId, bufferedImage, main50ImgLen, main50ImgWidth, true, "m50_aver_" + i, subName,
					filePath, "Y");

		}

		// for (int i = 0; i < imgList.length; i++) {
		// BufferedImage bufferedImage = ImageIO.read(new File("d:\\WWW.htdocs\\sample\\" + imgList[i]));
		// String prodId = "000000004";
		// String filePath = "d:\\WWW.htdocs\\";
		// String subName = "jpg";
		// resizePressJ(prodId, bufferedImage, main500ImgLen, main500ImgWidth, true, "m500_" + i, subName, filePath, "Y");
		// resizePressJ(prodId, bufferedImage, main250ImgLen, main250ImgWidth, true, "m250_" + i, subName, filePath, "Y");
		// resizePressJ(prodId, bufferedImage, main135ImgLen, main135ImgWidth, true, "m135_" + i, subName, filePath, "Y");
		// resizePressJ(prodId, bufferedImage, main100ImgLen, main100ImgWidth, true, "m100_" + i, subName, filePath, "Y");
		// resizePressJ(prodId, bufferedImage, main75ImgLen, main75ImgWidth, true, "m75_" + i, subName, filePath, "Y");
		// resizePressJ(prodId, bufferedImage, main50ImgLen, main50ImgWidth, true, "m50_" + i, subName, filePath, "Y");
		// }

	}

	/**
	 * 主檔大圖/中圖/小圖
	 * 
	 * @param filePath
	 * @param bb
	 */
	public static void mainResize(final String prodId, final BufferedImage bufferedImage, String subName, String filePath) {
		// resizePress(prodId, bufferedImage, main500ImgLen, main500ImgWidth, true, "m500", subName, filePath, "Y");
		// resizePress(prodId, bufferedImage, main250ImgLen, main250ImgWidth, true, "m250", subName, filePath, "Y");
		// resizePress(prodId, bufferedImage, main135ImgLen, main135ImgWidth, true, "m135", subName, filePath, "Y");
		// resizePress(prodId, bufferedImage, main100ImgLen, main100ImgWidth, true, "m100", subName, filePath, "Y");
		// resizePress(prodId, bufferedImage, main75ImgLen, main75ImgWidth, true, "m75", subName, filePath, "Y");
		// resizePress(prodId, bufferedImage, main50ImgLen, main50ImgWidth, true, "m50", subName, filePath, "Y");

		resizePressAveraging(prodId, bufferedImage, main500ImgLen, main500ImgWidth, true, "m500", subName, filePath, null);
		resizePressAveraging(prodId, bufferedImage, main250ImgLen, main250ImgWidth, true, "m250", subName, filePath, null);
		resizePressAveraging(prodId, bufferedImage, main135ImgLen, main135ImgWidth, true, "m135", subName, filePath, null);
		resizePressAveraging(prodId, bufferedImage, main100ImgLen, main100ImgWidth, true, "m100", subName, filePath, null);
		resizePressAveraging(prodId, bufferedImage, main75ImgLen, main75ImgWidth, true, "m75", subName, filePath, null);
		resizePressAveraging(prodId, bufferedImage, main50ImgLen, main50ImgWidth, true, "m50", subName, filePath, null);
	}

	/**
	 * 主檔大圖/中圖/小圖
	 * 
	 * @param filePath
	 * @param bb
	 */
	public static void mainResize2(final String prodId, final BufferedImage bufferedImage, String subName, String filePath) {
		// phase1、2先用500*500，3再改回400*400
		// resizePressAveraging(prodId, bufferedImage, main400ImgLen, main400ImgWidth, true, "m400", subName, filePath,
		// null);
		resizePressAveraging(prodId, bufferedImage, main500ImgLen, main500ImgWidth, true, "m500", subName, filePath, null);
		// resizePressAveraging(prodId, bufferedImage, main400ImgLen, main400ImgWidth, true, "m400", subName, filePath,
		// null);
		resizePressAveraging(prodId, bufferedImage, main250ImgLen, main250ImgWidth, true, "m250", subName, filePath, null);
		resizePressAveraging(prodId, bufferedImage, main135ImgLen, main135ImgWidth, true, "m135", subName, filePath, null);
		resizePressAveraging(prodId, bufferedImage, main100ImgLen, main100ImgWidth, true, "m100", subName, filePath, null);
		resizePressAveraging(prodId, bufferedImage, main75ImgLen, main75ImgWidth, true, "m75", subName, filePath, null);
		resizePressAveraging(prodId, bufferedImage, main50ImgLen, main50ImgWidth, true, "m50", subName, filePath, null);
	}

	/**
	 * 規格ICON圖
	 * 
	 * @param prodId
	 * @param bufferedImage
	 * @param subName
	 * @param filePath
	 * @param specId
	 */
	public static void iconResize(final String prodId, final BufferedImage bufferedImage, String subName,
			String filePath, String specId) {
		resizePressAveraging(prodId, bufferedImage, iconImgLen, iconImgWidth, true, "i40", subName, filePath, null, specId,
				"", 1);
	}

	public static void specResize(final String prodId, final BufferedImage bufferedImage, String subName,
			String filePath, String specId, String specTypeName, int showSeq) {
		if (StringUtils.equals("s75", specTypeName)) {
			resizePressAveraging(prodId, bufferedImage, mainSpecImgLen, mainSpecImgWidth, true, specTypeName, subName,
					filePath, null, specId, specTypeName, showSeq);
		}
		if (StringUtils.equals("rs99", specTypeName)) {
			resizePressAveraging(prodId, bufferedImage, rmainSpecImgLen, rmainSpecImgWidth, true, specTypeName, subName,
					filePath, null, specId, specTypeName, showSeq);
		}
	}

	public static void main400Resize(final String prodId, final BufferedImage bufferedImage, String subName,
			String filePath, String specId, int showSeq) {
		resizePressAveraging(prodId, bufferedImage, main400ImgLen, main400ImgWidth, true, "m400", subName, filePath, null,
				specId, "m400", showSeq);
	}

	public static void rmainResize(final String prodId, final BufferedImage bufferedImage, String subName, String filePath) {
		resizePressAveraging(prodId, bufferedImage, rmain530ImgLen, rmain400ImgWidth, true, "rm530", subName, filePath,
				null);
		resizePressAveraging(prodId, bufferedImage, rmain330ImgLen, rmain250ImgWidth, true, "rm330", subName, filePath,
				null);
		resizePressAveraging(prodId, bufferedImage, rmain225ImgLen, rmain170ImgWidth, true, "rm225", subName, filePath,
				null);
		resizePressAveraging(prodId, bufferedImage, rmain180ImgLen, rmain135ImgWidth, true, "rm180", subName, filePath,
				null);
		resizePressAveraging(prodId, bufferedImage, rmain130ImgLen, rmain100ImgWidth, true, "rm130", subName, filePath,
				null);
		resizePressAveraging(prodId, bufferedImage, rmain93ImgLen, rmain70ImgWidth, true, "rm93", subName, filePath, null);
	}

	public final static File pressImage(BufferedImage targetImg, int x, int y, float alpha, String filePath) {
		File img = new File(filePath);

		try {

			// Image src = ImageIO.read(img);
			// Image src = targetImg;
			int wideth = targetImg.getWidth(null);
			int height = targetImg.getHeight(null);
			BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);

			Graphics2D g = image.createGraphics();
			g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
			g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

			g.drawImage(targetImg, 0, 0, wideth, height, null);
			// 浮水印文件 -start
			logger.info("imgMfilePath:::{}", ServerValue.getMidApachePath() + "PImg/logo_ok_b.png");
			BufferedImage src_biao = ImageIO.read(new File(ServerValue.getMidApachePath() + "PImg/logo_ok_b.png"));
			// Image src_biao = ImageIO.read(new File(pressImg));
			int wideth_biao = src_biao.getWidth(null);
			int height_biao = src_biao.getHeight(null);

			int pos_x = Long.valueOf(Math.round((wideth * 0.25) - (wideth_biao / 2))).intValue();
			int pos_y = Long.valueOf(Math.round(height * 0.75 - (height_biao / 2))).intValue();

			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
			g.drawImage(src_biao, pos_x, pos_y, wideth_biao, height_biao, null);
			logger.info("pos:::{}", pos_x + "," + pos_y);
			// 浮水印文件 -end
			g.dispose();

			Iterator<ImageWriter> it_a = ImageIO.getImageWritersBySuffix("jpg");
			if (it_a.hasNext()) {
				FileImageOutputStream fileImageOutputStream = new FileImageOutputStream(img);
				ImageWriter iw = it_a.next();
				ImageWriteParam iwp = iw.getDefaultWriteParam();
				iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
				iwp.setCompressionQuality(compressRatio);
				iw.setOutput(fileImageOutputStream);
				iw.write(null, new IIOImage(image, null, null), iwp);
				iw.dispose();
				fileImageOutputStream.flush();
				fileImageOutputStream.close();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return img;
	}

	/**
	 * 圖片水印
	 * 
	 * @param pressImg 水印圖片
	 * @param targetImg 目標圖片
	 * @param x 修正值 默認在中間
	 * @param y 修正值 默認在中間
	 * @param alpha 透明度
	 */
	public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) {
		try {
			File img = new File(targetImg);
			Image src = ImageIO.read(img);
			int wideth = src.getWidth(null);
			int height = src.getHeight(null);
			BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);

			Graphics2D g = image.createGraphics();
			g.drawImage(src, 0, 0, wideth, height, null);
			// 浮水印文件 -start
			Image src_biao = ImageIO.read(new File(pressImg));
			int wideth_biao = src_biao.getWidth(null);
			int height_biao = src_biao.getHeight(null);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
			g.drawImage(src_biao, (wideth - wideth_biao) / 1, (height - height_biao) / 1, wideth_biao, height_biao, null);
			// 浮水印文件 -end
			g.dispose();
			ImageIO.write(image, "jpg", img);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 文字水印
	 * 
	 * @param pressText 水印文字
	 * @param targetImg 目標圖片
	 * @param fontName 字體名稱
	 * @param fontStyle 字體樣式
	 * @param color 字體顏色
	 * @param fontSize 字體大小
	 * @param x 修正值
	 * @param y 修正值
	 * @param alpha 透明度
	 */
	public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color,
			int fontSize, int x, int y, float alpha) {
		try {
			File img = new File(targetImg);
			Image src = ImageIO.read(img);
			int width = src.getWidth(null);
			int height = src.getHeight(null);
			BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics2D g = image.createGraphics();
			g.drawImage(src, 0, 0, width, height, null);
			g.setColor(color);
			g.setFont(new Font(fontName, fontStyle, fontSize));
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
			g.drawString(pressText, (width - (getLength(pressText) * fontSize)) / 2 + x, (height - fontSize) / 2 + y);
			g.dispose();
			ImageIO.write(image, "jpg", img);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static BufferedImage resize(BufferedImage oldImg, int width, int height) {
		BufferedImage newImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D graph = newImg.createGraphics();

		// way1
		// graph.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		// graph.drawImage(oldImg, 0, 0, width, height, null);
		// way2
		AffineTransform xform = AffineTransform.getScaleInstance(width / oldImg.getWidth(), height / oldImg.getHeight());
		graph.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
		graph.drawImage(oldImg, xform, null);

		graph.dispose();
		return newImg;
	}

	/**
	 * 縮放
	 * 
	 * @param filePath 圖片路徑
	 * @param height 高度
	 * @param width 寬度
	 * @param bb 比例不對時是否需要補白
	 */
	public static void resize(String prodId, BufferedImage bufferedImage, int height, int width, boolean bb, String name,
			String subName, String filePath) {
		try {
			double ratio = 0.0; // 縮放比例
			BufferedImage bi = bufferedImage;
			Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
			// 計算比例
			if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
				if (bi.getHeight() > bi.getWidth()) {
					ratio = Integer.valueOf(height).doubleValue() / bi.getHeight();
				} else {
					ratio = Integer.valueOf(width).doubleValue() / bi.getWidth();
				}
				AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
				itemp = op.filter(bi, null);
			}
			if (bb) {
				BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
				Graphics2D g = image.createGraphics();
				g.setColor(Color.white);
				g.fillRect(0, 0, width, height);
				if (width == itemp.getWidth(null)) {
					g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null),
							Color.white, null);
				} else {
					g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null),
							Color.white, null);
				}
				g.dispose();
				itemp = image;
			}
			// 因有目錄數限制,所以增加以前商品4碼作分類目錄
			File newFourFolder = new File(filePath + prodId.substring(0, 4));
			if (!newFourFolder.exists()) {
				newFourFolder.mkdir();
			}
			File newFolder = new File(filePath + prodId.substring(0, 4) + File.separator + prodId);
			if (!newFolder.exists()) {
				newFolder.mkdir();
			}
			ImageIO.write((BufferedImage) itemp, subName.toUpperCase(), new File(newFolder.getPath() + File.separator
					+ prodId + "_" + name + "." + subName));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 縮放
	 * 
	 * @param prodId
	 * @param bufferedImage
	 * @param height 高度
	 * @param width 寬度
	 * @param bb 比例不對時是否需要補白
	 * @param name
	 * @param subName
	 * @param filePath 圖片路徑
	 * @param pressImage 浮水印路徑
	 */
	public static void resizePress(String prodId, BufferedImage bufferedImage, int height, int width, boolean bb,
			String name, String subName, String filePath, String pressImage) {

		try {
			// 因有目錄數限制,所以增加以前商品4碼作分類目錄
			File newFourFolder = new File(filePath + prodId.substring(0, 4));
			if (!newFourFolder.exists()) {
				newFourFolder.mkdir();
			}
			File newFolder = new File(filePath + prodId.substring(0, 4) + File.separator + prodId);
			if (!newFolder.exists()) {
				newFolder.mkdir();
			}
			File file = null;
			BufferedImage bi = bufferedImage;
			if (StringUtils.isNotBlank(pressImage)) {// 浮水印
				file = pressImage(bufferedImage, 0, 0, 1f, newFolder.getAbsolutePath() + File.separator + prodId + "_" + name
						+ ".jpg");
				bi = ImageIO.read(file);
			}
			double ratio = 0.0; // 縮放比例

			Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
			// 計算比例

			double diffH = Integer.valueOf(height).doubleValue() / Integer.valueOf(bi.getHeight()).doubleValue();
			double diffW = Integer.valueOf(width).doubleValue() / Integer.valueOf(bi.getWidth()).doubleValue();

			if (diffH < diffW) {

				ratio = Integer.valueOf(height).doubleValue() / bi.getHeight();

				AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio),
						AffineTransformOp.TYPE_BILINEAR);
				itemp = op.filter(bi, null);
			} else {
				ratio = Integer.valueOf(width).doubleValue() / bi.getWidth();

				AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio),
						AffineTransformOp.TYPE_BILINEAR);
				itemp = op.filter(bi, null);
			}
			if (bb) {
				BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
				Graphics2D g = image.createGraphics();
				g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
				g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
				g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g.setColor(Color.white);
				g.fillRect(0, 0, width, height);
				if (width == itemp.getWidth(null)) {
					g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null),
							Color.white, null);
				} else {
					g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null),
							Color.white, null);
				}
				g.dispose();
				itemp = image;
			} else {
				BufferedImage image = new BufferedImage(itemp.getWidth(null), itemp.getHeight(null), BufferedImage.TYPE_INT_RGB);
				Graphics2D g = image.createGraphics();
				g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
				g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
				g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g.setColor(Color.white);
				g.fillRect(0, 0, image.getWidth(), image.getHeight());
				g.drawImage(itemp, 0, 0, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
				g.dispose();
				itemp = image;
			}

			ImageIO.write((BufferedImage) itemp, subName.toUpperCase(), new File(newFolder.getPath() + File.separator
					+ prodId + "_" + name + "." + subName));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void resizePressAveraging(String prodId, BufferedImage bufferedImage, int height, int width,
			boolean bb, String name, String subName, String filePath, String pressImage) {
		resizePressAveraging(prodId, bufferedImage, height, width, bb, name, subName, filePath, pressImage, "", "", 0);
	}

	public static void resizePressAveraging(String prodId, BufferedImage bufferedImage, int height, int width,
			boolean bb, String name, String subName, String filePath, String pressImage, String specTypeName) {
		resizePressAveraging(prodId, bufferedImage, height, width, bb, name, subName, filePath, pressImage, "", "", 0);
	}

	public static void resizePressAveraging(String prodId, BufferedImage bufferedImage, int height, int width,
			boolean bb, String name, String subName, String filePath, String pressImage, String specId, String specTypeName,
			int showSeq) {
		// try {
		// 因有目錄數限制,所以增加以前商品4碼作分類目錄
		File newFourFolder = new File(filePath + prodId.substring(0, 4));
		if (!newFourFolder.exists()) {
			newFourFolder.mkdir();
		}
		File newFolder = new File(filePath + prodId.substring(0, 4) + File.separator + prodId);
		if (!newFolder.exists()) {
			newFolder.mkdir();
		}

		boolean waterMark = false;
		if (StringUtils.isNotBlank(pressImage)) {
			waterMark = true;
		}
		if (StringUtils.isBlank(specId)) {
			customChange(width, height, bb, bufferedImage, newFolder.getPath() + File.separator + prodId + "_" + name + "."
					+ subName, waterMark);
		} else {
			logger.info("specTypeName:{}", specTypeName);
			if (StringUtils.isBlank(specTypeName)) {
				customChange(width, height, bb, bufferedImage, newFolder.getPath() + File.separator + prodId + "_" + specId
						+ "_" + name + "." + subName, waterMark);
			} else {
				customChange(width, height, bb, bufferedImage, newFolder.getPath() + File.separator + prodId + "_" + specId
						+ "_" + showSeq + "_" + name + "." + subName, waterMark);
			}
		}
	}
}
